import java.util.StringTokenizer;

class P469_39643 {

	public static void main(String[] args) {
		int cases = Integer.parseInt(readLn());
		readLn();
		for (int k = 0; k < cases; k++) {
			process();
			
			if (k + 1 != cases) System.out.println();
			
		}
		
	}

	static void process() {
		
		char[][] land = new char[99][99];
		
		String line = readLn();
		
		int iLength = 0;
		int jLength = 0;
		
		while (true) {
			
			if (line.charAt(0) >= '0' && line.charAt(0) <= '9') break;
			
			jLength = 0;
			for (int k = 0; k < line.length(); k++) {
				land[iLength][k] = line.charAt(k);
				jLength++;
			}
			
			iLength++;
			
			line = readLn();
		}
		
		while ( line != null && !"".equals(line)) {
			StringTokenizer st = new StringTokenizer(line);
			int i = Integer.parseInt(st.nextToken());
			int j = Integer.parseInt(st.nextToken());
			
			System.out.println(process(land, iLength, jLength, i-1, j-1));
			
			for (i = 0; i < iLength; i++ ) {
				for (j = 0; j < jLength; j++) {
					if (land[i][j] == 'M') land[i][j] = 'W';
				}
			}
			
			line = readLn();
		}
		
	}
	
	private static int process(char[][] land, int iLength, int jLength, int i, int j) {
		
		if (j >= 0 && j < jLength && i >= 0 && i < iLength) {
			
			if (land[i][j] != 'W') return 0;
			
			land[i][j] = 'M';
			
			return 1 + process(land, iLength, jLength, i, j + 1) +
					process(land, iLength, jLength, i, j - 1) +
					process(land, iLength, jLength, i + 1, j + 1) +
					process(land, iLength, jLength, i + 1, j ) +
					process(land, iLength, jLength, i + 1, j - 1) +
					process(land, iLength, jLength, i - 1, j + 1 ) +
					process(land, iLength, jLength, i - 1, j ) +
					process(land, iLength, jLength, i - 1, j - 1 );
		}
		
		return 0;
		
	}

	static String readLn() {
		String newLine = System.getProperty("line.separator");
		StringBuffer buffer = new StringBuffer();
		int car = -1;
		try {
			car = System.in.read();
			while ((car > 0) && (car != newLine.charAt(0))) {
				buffer.append((char) car);
				car = System.in.read();
			}
			if (car == newLine.charAt(0))
				System.in.skip(newLine.length() - 1);
		} catch (java.io.IOException e) {
			return (null);
		}
		if ((car < 0) && (buffer.length() == 0))
			return (null);
		return (buffer.toString()).trim();
	}

}
